<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>MongoDB PHP7 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>MongoDB PHP7</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MongoDB/">MongoDB</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本文教程只适合在 PHP7 的环境，如果是 PHP5 环境，可以参阅 <a href="https://hcldirgit.github.io/2017/08/27/MongoDB/8.%20MongoDB%20PHP/" target="_blank" rel="external">MongoDB PHP</a></p>
<h2 id="PHP7-Mongdb-扩展安装"><a href="#PHP7-Mongdb-扩展安装" class="headerlink" title="PHP7 Mongdb 扩展安装"></a>PHP7 Mongdb 扩展安装</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 pecl 命令来安装：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ /usr/<span class="built_in">local</span>/php7/bin/pecl install mongodb</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行成功后，会输出以下结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">……</div><div class="line">Build process completed successfully</div><div class="line">Installing <span class="string">'/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/mongodb.so'</span></div><div class="line">install ok: channel://pecl.php.net/mongodb-1.1.7</div><div class="line">configuration option <span class="string">"php_ini"</span> is not <span class="built_in">set</span> to php.ini location</div><div class="line">You should add <span class="string">"extension=mongodb.so"</span> to php.ini</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来打开 php.ini 文件，添加 <strong>extension=mongodb.so</strong> 配置。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以直接执行以下命令来添加。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ <span class="built_in">echo</span> <span class="string">"extension=mongodb.so"</span> &gt;&gt; `/usr/<span class="built_in">local</span>/php7/bin/php --ini | grep <span class="string">"Loaded Configuration"</span> | sed -e <span class="string">"s|.*:\s*||"</span>`</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>注意</strong>：<em>以上执行的命令中 php7 的安装目录为 /usr/local/php7/，如果你安装在其他目录，需要相应修改 pecl 与 php 命令的路径。</em></p>
<h2 id="Mongodb-使用"><a href="#Mongodb-使用" class="headerlink" title="Mongodb 使用"></a>Mongodb 使用</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;PHP7 连接 MongoDB 语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$manager</span> = new MongoDB\Driver\Manager(<span class="string">"mongodb://localhost:27017"</span>);</div></pre></td></tr></table></figure>
<h3 id="插入数据"><a href="#插入数据" class="headerlink" title="插入数据"></a>插入数据</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将 name 为”菜鸟教程” 的数据插入到 test 数据库的 runoob 集合中。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$bulk = <span class="keyword">new</span> MongoDB\Driver\BulkWrite;</div><div class="line">$document = [<span class="string">'_id'</span> =&gt; <span class="keyword">new</span> MongoDB\BSON\ObjectID, <span class="string">'name'</span> =&gt; <span class="string">'菜鸟教程'</span>];</div><div class="line"> </div><div class="line">$_id= $bulk-&gt;insert($document);</div><div class="line"> </div><div class="line">var_dump($_id);</div><div class="line"> </div><div class="line">$manager = <span class="keyword">new</span> MongoDB\Driver\Manager(<span class="string">"mongodb://localhost:27017"</span>); </div><div class="line">$writeConcern = <span class="keyword">new</span> MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, <span class="number">1000</span>);</div><div class="line">$result = $manager-&gt;executeBulkWrite(<span class="string">'test.runoob'</span>, $bulk, $writeConcern);</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<h3 id="读取数据"><a href="#读取数据" class="headerlink" title="读取数据"></a>读取数据</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里将三个网址数据插入到 test 数据库的 sites 集合，并读取迭代出来：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$manager = <span class="keyword">new</span> MongoDB\Driver\Manager(<span class="string">"mongodb://localhost:27017"</span>); </div><div class="line"></div><div class="line"><span class="comment">// 插入数据</span></div><div class="line">$bulk = <span class="keyword">new</span> MongoDB\Driver\BulkWrite;</div><div class="line">$bulk-&gt;insert([<span class="string">'x'</span> =&gt; <span class="number">1</span>, <span class="string">'name'</span>=&gt;<span class="string">'菜鸟教程'</span>, <span class="string">'url'</span> =&gt; <span class="string">'http://www.runoob.com'</span>]);</div><div class="line">$bulk-&gt;insert([<span class="string">'x'</span> =&gt; <span class="number">2</span>, <span class="string">'name'</span>=&gt;<span class="string">'Google'</span>, <span class="string">'url'</span> =&gt; <span class="string">'http://www.google.com'</span>]);</div><div class="line">$bulk-&gt;insert([<span class="string">'x'</span> =&gt; <span class="number">3</span>, <span class="string">'name'</span>=&gt;<span class="string">'taobao'</span>, <span class="string">'url'</span> =&gt; <span class="string">'http://www.taobao.com'</span>]);</div><div class="line">$manager-&gt;executeBulkWrite(<span class="string">'test.sites'</span>, $bulk);</div><div class="line"></div><div class="line">$filter = [<span class="string">'x'</span> =&gt; [<span class="string">'$gt'</span> =&gt; <span class="number">1</span>]];</div><div class="line">$options = [</div><div class="line">    <span class="string">'projection'</span> =&gt; [<span class="string">'_id'</span> =&gt; <span class="number">0</span>],</div><div class="line">    <span class="string">'sort'</span> =&gt; [<span class="string">'x'</span> =&gt; <span class="number">-1</span>],</div><div class="line">];</div><div class="line"></div><div class="line"><span class="comment">// 查询数据</span></div><div class="line">$query = <span class="keyword">new</span> MongoDB\Driver\Query($filter, $options);</div><div class="line">$cursor = $manager-&gt;executeQuery(<span class="string">'test.sites'</span>, $query);</div><div class="line"></div><div class="line"><span class="keyword">foreach</span> ($cursor <span class="keyword">as</span> $document) &#123;</div><div class="line">    print_r($document);</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">stdClass Object</div><div class="line">(</div><div class="line">    [x] =&gt; 3</div><div class="line">    [name] =&gt; taobao</div><div class="line">    [url] =&gt; http://www.taobao.com</div><div class="line">)</div><div class="line">stdClass Object</div><div class="line">(</div><div class="line">    [x] =&gt; 2</div><div class="line">    [name] =&gt; Google</div><div class="line">    [url] =&gt; http://www.google.com</div><div class="line">)</div></pre></td></tr></table></figure>
<h3 id="更新数据"><a href="#更新数据" class="headerlink" title="更新数据"></a>更新数据</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来我们将更新 test 数据库 sites 集合中 x 为 2 的数据：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$bulk = <span class="keyword">new</span> MongoDB\Driver\BulkWrite;</div><div class="line">$bulk-&gt;update(</div><div class="line">    [<span class="string">'x'</span> =&gt; <span class="number">2</span>],</div><div class="line">    [<span class="string">'$set'</span> =&gt; [<span class="string">'name'</span> =&gt; <span class="string">'菜鸟工具'</span>, <span class="string">'url'</span> =&gt; <span class="string">'tool.runoob.com'</span>]],</div><div class="line">    [<span class="string">'multi'</span> =&gt; <span class="keyword">false</span>, <span class="string">'upsert'</span> =&gt; <span class="keyword">false</span>]</div><div class="line">);</div><div class="line"></div><div class="line">$manager = <span class="keyword">new</span> MongoDB\Driver\Manager(<span class="string">"mongodb://localhost:27017"</span>); </div><div class="line">$writeConcern = <span class="keyword">new</span> MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, <span class="number">1000</span>);</div><div class="line">$result = $manager-&gt;executeBulkWrite(<span class="string">'test.sites'</span>, $bulk, $writeConcern);</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来我们使用 “db.sites.find()” 命令查看数据的变化，x 为 2 的数据已经变成了菜鸟工具：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20PHP7/01.jpeg?raw=true" alt=""></figure></p>
<h3 id="删除数据"><a href="#删除数据" class="headerlink" title="删除数据"></a>删除数据</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下实例删除了 x 为 1 和 x 为 2的数据，注意 limit 参数的区别：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$bulk = <span class="keyword">new</span> MongoDB\Driver\BulkWrite;</div><div class="line">$bulk-&gt;delete([<span class="string">'x'</span> =&gt; <span class="number">1</span>], [<span class="string">'limit'</span> =&gt; <span class="number">1</span>]); <span class="comment">// limit 为 1 时，删除第一条匹配数据</span></div><div class="line">$bulk-&gt;delete([<span class="string">'x'</span> =&gt; <span class="number">2</span>], [<span class="string">'limit'</span> =&gt; <span class="number">0</span>]); <span class="comment">// limit 为 0 时，删除所有匹配数据</span></div><div class="line"> </div><div class="line">$manager = <span class="keyword">new</span> MongoDB\Driver\Manager(<span class="string">"mongodb://localhost:27017"</span>); </div><div class="line">$writeConcern = <span class="keyword">new</span> MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, <span class="number">1000</span>);</div><div class="line">$result = $manager-&gt;executeBulkWrite(<span class="string">'test.sites'</span>, $bulk, $writeConcern);</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更多使用方法请参考：<a href="http://php.net/manual/en/book.mongodb.php。" target="_blank" rel="external">http://php.net/manual/en/book.mongodb.php。</a></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/MongoDB/9. MongoDB PHP7/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
